<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<h2>{{ $ctrl.getTitle() }}</h2>
<div id="api-keys" class="gv-form-content" layout="column">
  <ng-banner ng-if="$ctrl.isSharedApiKey() && !$ctrl.areKeysEditable()">
    This subscription uses a shared API Key.<br />
    You can renew or revoke the shared API Key at the application level.
  </ng-banner>
  <md-table-container>
    <table md-table>
      <thead md-head>
        <tr md-row>
          <th md-column>Key</th>
          <th md-column>Created at</th>
          <th md-column>Revoked / Expire at</th>
          <th md-column></th>
        </tr>
      </thead>
      <tbody md-body>
        <tr md-row ng-repeat="key in $ctrl.keys | orderBy:['-revoked_at','-expire_at','created_at'] track by key.key ">
          <td md-cell class="api-key-cell">
            <ng-md-icon
              icon="{{$ctrl.isValid(key)?'done':'clear'}}"
              ng-class="$ctrl.isValid(key)? 'active-icon' : 'revoked-icon'"
            ></ng-md-icon>
            <code>{{key.key}}</code>
            <span ng-if="$ctrl.isValid(key) && $ctrl.areKeysEditable()">
              <md-tooltip md-direction="right">Copy to clipboard</md-tooltip>
              <ng-md-icon
                icon="content_copy"
                ngclipboard
                data-clipboard-text="{{key.key}}"
                ngclipboard-success="$ctrl.onCopyApiKeySuccess(e);"
                role="button"
              ></ng-md-icon>
            </span>
          </td>
          <td md-cell>{{key.created_at | date:'yyyy-MM-dd HH:mm:ss'}}</td>
          <td md-cell>{{key.revoked_at || key.expire_at | date:'yyyy-MM-dd HH:mm:ss'}}</td>
          <td md-cell>
            <span ng-if="$ctrl.isValid(key) && $ctrl.areKeysEditable()">
              <md-tooltip md-direction="left">Revoke</md-tooltip>
              <ng-md-icon
                class="revoke-icon"
                permission
                permission-only="'application-subscription-u'"
                icon="not_interested"
                ng-click="$ctrl.revokeApiKey(key)"
              ></ng-md-icon>
            </span>
          </td>
        </tr>
      </tbody>
    </table>
  </md-table-container>

  <div
    class="md-actions gravitee-api-save-button"
    layout="row"
    permission
    permission-only="'application-subscription-u'"
    ng-if="$ctrl.areKeysEditable()"
  >
    <md-button class="md-raised md-primary" ng-click="$ctrl.renewApiKey()">
      <ng-md-icon class="renew-icon" icon="autorenew"></ng-md-icon>
      Renew API Key
    </md-button>
  </div>
</div>
